package br.ufms.facom;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.UnknownHostException;




import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class TrucoBT extends TrucoBTActivity {

	private OutputStream mOutputStream;
	private PrintWriter mPrintWriterOUT;
	private BufferedReader mBufferedReader; 
	
	private static BluetoothSocket mSocket;
	

	private void initBT()
	{
	
		
		try {

				mBufferedReader=new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
				mOutputStream=mSocket.getOutputStream();
				mPrintWriterOUT=new PrintWriter(mOutputStream);
			} catch (UnknownHostException e) {
				
				Toast.makeText(TrucoBT.this, "UNKNOWN SERVER ADDRESS", 100).show();
			} catch (IOException e) {
				
				Toast.makeText(TrucoBT.this, "ERROR CREATING SOCKET", 100).show();
			}
		
		
		 
	}

	@Override
	public void CarregaLayout() {

		initBT();
		Log.i("Filho", "load");
		super.CarregaLayout();

	}
	
	
	@Override
	public String receiveMessage()
	{

		Log.i("Filho", "receive");

		String receivedMessage;
		try {
			receivedMessage = new String(mBufferedReader.readLine()+ "\n");
			receivedMessage.trim();
			return receivedMessage;
		} catch (IOException e) {
			Log.e("Multiplayer_BT", "error reading stream");
			isRunning=false;
		}

		return null;
		
	}
	
	@Override
	public void sendMessage(String str) {

		mPrintWriterOUT.println(str);
		mPrintWriterOUT.flush();
		Log.i("Filho", "send");
	}

	
	public static void setBluetoothSocket(BluetoothSocket _BTSocket)
	{
		mSocket=_BTSocket;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}
	

}